package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.entity.College;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.ScoreIntegral;
import cc.zy.base.hr.mapper.ScoreIntegralMapper;
import cc.zy.base.hr.service.IScoreIntegralService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 标签
 * Service
 *
 * @author ...
 * @date 2021-01-18 10:51:13
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ScoreIntegralServiceImpl extends ServiceImpl<ScoreIntegralMapper, ScoreIntegral> implements IScoreIntegralService {

    /**
     * 用于设定添加记录时的id自增
     *
     */
    private final Integer scoreIntegralId = 0;

    @Resource
    private final ScoreIntegralMapper scoreIntegralMapper;

    /**
     * 查询（分页）
     *
     */
    @Override
    public IPage<ScoreIntegral> findScoreIntegrals(QueryRequest request, ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        Page<ScoreIntegral> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countScoreIntegralDetail(scoreIntegral));

        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findScoreIntegralDetailPage(page, scoreIntegral);
    }

    /**
     * 通过ID查找积分详细信息
     *
     */
    @Override
    public ScoreIntegral findScoreIntegralDetailList(Integer id) {

        log.debug("索要查找的id为：" + id);

        ScoreIntegral scoreIntegral = new ScoreIntegral();
        scoreIntegral.setId(id);

        List<ScoreIntegral> scoreIntegralDetail = baseMapper.findScoreIntegralDetail(scoreIntegral);
        return CollectionUtils.isNotEmpty(scoreIntegralDetail) ? scoreIntegralDetail.get(0) : null;
    }

    /**
     * 通过ID查找积分详细信息
     *
     */
    @Override
    public ScoreIntegral findByScoreIntegralId(Integer id) {
        return baseMapper.findByScoreIntegralId(id);
    }

    /**
     * 查询（所有）
     *
     */
    @Override
    public List<ScoreIntegral> findScoreIntegrals(ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        LambdaQueryWrapper<ScoreIntegral> queryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createScoreIntegral(ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        scoreIntegral.setCreateDate(new Date());
        scoreIntegral.setId(scoreIntegralMapper.findScoreIntegralNullId() + 1);

        /**
         * 暂定（根据具体考试成绩从积分映射表获取数据填）
         */
        scoreIntegral.setMappingRemark("以下随便写点先");
        scoreIntegral.setMappingMinValue(0.1);
        scoreIntegral.setMappingMaxValue(0.1);
        scoreIntegral.setIntegralValue(25.0);
        scoreIntegral.setMappingResult(25.0);
        /**
         * 暂定结束
         */
        this.save(scoreIntegral);
    }

    /**
     * 修改
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScoreIntegral(ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        this.saveOrUpdate(scoreIntegral);
    }

    /**
     * 删除
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteScoreIntegral(ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        LambdaQueryWrapper<ScoreIntegral> wrapper = new LambdaQueryWrapper<>();
        this.remove(wrapper);
    }

    /**
     * 批量删除
     *
     */
    @Override
    public void deleteScoreIntegrals(String[] scoreIntegralIds) {

        log.debug("labelIDs：" + scoreIntegralIds);

        List<String> list = Arrays.asList(scoreIntegralIds);
        this.removeByIds(list);
    }

    /**
     * 查询不带分页
     *
     */
    @Override
    public List<ScoreIntegral> findScoreIntegralListNotPage(ScoreIntegral scoreIntegral) {

        log.debug("从前端获取的参数为：" + scoreIntegral);

        List<ScoreIntegral> scoreIntegralListNotPage = baseMapper.findScoreIntegralNotPage(scoreIntegral);
        return scoreIntegralListNotPage;
    }

    /**
     * 查询前台输入的全称或简称是否存在
     *
     */
    @Override
    public Boolean isRepetitive(Integer staffId, String scoreDesc) {

        List<ScoreIntegral> repetitive = scoreIntegralMapper.isRepetitive(staffId, scoreDesc);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public Integer findScoreIntegralNullId() {

        Integer nullId = scoreIntegralMapper.findScoreIntegralNullId();
        return nullId;
    }
}
